package com.itgaoshu.hospitalproject.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itgaoshu.hospitalproject.bean.Upplier;
import com.itgaoshu.hospitalproject.service.UpplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("upplier")
public class UpplierController {

    @Autowired
    private UpplierService upplierService;

    //查询所有供货商的方法
    @RequestMapping("findAllUpplier")
    @ResponseBody
    public Object findAllUpplier(String supplierName,Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        List<Upplier> uppliers = upplierService.findAllUpplier(supplierName);
        PageInfo pageInfo = new PageInfo(uppliers);
        Map<String,Object> tableData = new HashMap<>();
        tableData.put("msg","");
        tableData.put("code",0);
        tableData.put("count",pageInfo.getTotal());
        tableData.put("data",pageInfo.getList());
        return tableData;
    }


    //添加供货商的方法
    @ResponseBody
    @RequestMapping("addUpplier")
    public Object addUpplier(String supplierName){
        int count = upplierService.count(supplierName);
        if(count==0){
            int result = upplierService.addUpplier(supplierName);
            if(result>0){
                return "添加成功";
            }else{
                return "添加失败";
            }
        }else{
            return "这个供货商已经存在了哦";
        }
    }


    //删除供货商的方法
    @RequestMapping("deleteUpplier")
    @ResponseBody
    public Object deleteUpplier(Integer supplierId){
        int result = upplierService.deleteUpplier(supplierId);
        if(result>0){
            return "删除成功";
        }else{
            return "删除失败";
        }
    }

}
